/**
  ******************************************************************************
  * @file           : route_common.h
  * @brief          : Header file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ROUTE_BASE_H
#define __ROUTE_BASE_H

#include <stdint.h>

#define ROUTE_DID_STRLEN           13
#define ROUTE_DID_BUFLEN           16

#define ROUTE_PAYLOAD_MAXLEN       136

#define ROUTE_DEVADDR_MASK        (0xC0u)

#define idx2add(x)                (x | ROUTE_DEVADDR_MASK)
#define add2idx(x)                (x & (~ROUTE_DEVADDR_MASK))

#define ROUTE_PLCRC_CHECK         0
#define ROUTE_PLCRC_WRITE         1

/* 根类型:表示报文的方向和路径 */
typedef enum
{
  ROUTE_TRSDIR_NET_TO_LOCAL,
  ROUTE_TRSDIR_LOCAL_TO_NET,
  ROUTE_TRSDIR_NET_TO_SUBDEV,
  ROUTE_TRSDIR_SUBDEV_TO_NET,
  ROUTE_TRSDIR_LOCAL_TO_SUBDEV,
  ROUTE_TRSDIR_SUBDEV_TO_LOCAL,
  ROUTE_TRSDIR_LOCAL_CONFIG,     /* 特殊传输方向,用于网关本地配置的,不做平台交互,搭配 特殊设备类型(RT_DV_GWCFG) */
}Route_TransDir_Type;

/* 设备类型:表示报文的设备特有性 */
typedef enum
{
  RT_DV_UN    = 0,
  RT_DV_GW    = 1u,
  RT_DV_HK    = 5u,
  RT_DV_XC    = 7u,
  RT_DV_LX    = 8u,
  RT_DV_ZL    = 9u,
  RT_DV_CF    = 10u,
  RT_DV_SP    = 11u,
  RT_DV_SW    = 12u,
  RT_DV_SD    = 13u,
  RT_DV_DK    = 14u,
  RT_DV_GWCFG = 235u,   /* 特殊设备类型,用于网关本地配置的,不做平台交互 */
}Route_DevType_Type;

typedef enum
{
  ROUTE_PLTYP_UN_ERROR   = 0xF0u,
  ROUTE_PLTYP_UN_BCTIME  = 0xF2u,
	ROUTE_PLTYP_UN_PING    = 0xF4u,
  ROUTE_PLTYP_UN_RESPING = 0xF5u,
}Route_PlType_Un_Type;

typedef struct
{
  uint8_t msg;
}Route_pl_un_msg_Type;

typedef struct
{
	uint8_t  dev_addr;      /* 设备地址 */
	uint8_t  dev_type;      /* 设备类型 */
	uint8_t  trans_dir;     /* 传输方向 */
	uint8_t  pl_type;       /* 负载类型 */
  uint16_t pl_len;        /* 负载长度 */
  uint16_t crc16;         /* crc 校验 */
  uint8_t  payload[ROUTE_PAYLOAD_MAXLEN];
}Route_PayloadType;

typedef struct
{
	uint8_t            year;
	uint8_t            month;
  uint8_t            devindex;
	uint32_t           sequence;
	Route_DevType_Type type;
}Route_DidFormatType;

extern uint16_t crc16(const uint8_t *buf, uint32_t len);
extern uint8_t Route_PayloadCRC(Route_PayloadType *rtpl, uint8_t type);
extern Route_DevType_Type Route_DidToType(const char* did);
extern uint8_t Route_ParseDID(const char *did, Route_DidFormatType *did_format);

#endif /* __ROUTE_BASE_H */

